#ifndef CLIENTSEARCH_H
#define CLIENTSEARCH_H

#include <QWidget>
#include <QRadioButton>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QString>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlResult>
#include <QTableView>
#include <QDebug>
#include "pushbutton.h"
#include "lineedit.h"

class SqlQueryModel : public QSqlQueryModel
{
    QString query_type;
public:
    Qt::ItemFlags flags(const QModelIndex &index) const
    {
        Qt::ItemFlags flags = QSqlQueryModel::flags(index);
        return (flags |= Qt::ItemIsEditable);
    }
    bool setData(const QModelIndex &index, const QVariant &value, int role)
    {
        int id = QSqlQueryModel::data(QSqlQueryModel::index(index.row(), 1)).toInt();
        int column = index.column();
        QString column_name, table_name = "clients";
        switch(column) {
        case 0 :
            column_name = "uid";
            break;
        case 1 :
            column_name = "id";
            break;
        case 2 :
            column_name = "inicial";
            break;
        case 3 :
            column_name = "street";
            break;
        case 4 :
            column_name = "address";
            break;
        case 5 :
            column_name = "sector_bulding";
            break;
        case 6 :
            column_name = "home_phone";
            break;
        case 7 :
            column_name = "mobile_phone";
            break;
        case 8 :
            column_name = "service_type";
            break;
        case 9 :
            column_name = "type_machine";
            break;
        case 10 :
            column_name = "port_number";
            break;
        case 11 :
            column_name = "begin_service";
            break;
        case 12 :
            column_name = "notation";
            break;
        case 13 :
            column_name = "username";
            break;
        case 14 :
            column_name = "value";
            break;
        }
        QSqlQuery query;
        if ((column_name == "username") || (column_name == "value")) table_name = "radcheck";
        query.exec(QString("update %1 set %2='%3' where id=%4;").arg(table_name).arg(column_name).arg(value.toString()).arg(id));
        clear();
        if (query_type == "all") setQuery("select clients.*, radcheck.username, radcheck.value from clients, radcheck"
                                          " where clients.id=radcheck.id;");
        else setQuery(QString("select clients.*, radcheck.username, radcheck.value from clients, radcheck where clients.id=%1 and"
                              " clients.id=radcheck.id;").arg(id));
    }
    void setQueryType(QString q_t)
    {
        query_type = q_t;
    }
};

class UserSearch : public QWidget
{
    Q_OBJECT

    LineEdit *search_line;
    QRadioButton *name_search, *login_search, *id_search, *phone_search, *street_search, *dev_type_search, *tos_search,
    *num_port_search, *all_search;
    PushButton *param_search_but, *search_but, *mode_but;
    QGroupBox *g_b;
    SqlQueryModel *model;
    QTableView *database_view, *database_blocked_view, *database_common_view;
    QString query;
public:
    UserSearch(QWidget* = 0);
    ~UserSearch();
protected:
    void mousePressEvent(QMouseEvent *);
public slots:
    void paramSearchWid();
    void enableSearchBut();
    void setSearchParam();
    void userSearch();
    void modeSearch();
};

#endif // CLIENTSEARCH_H
